<?php
declare (strict_types = 1);

namespace ui;

/**
 * 
 */
class FormField {

    public const TYPE_INPUT = "INPUT";
    public const TYPE_SELECT = "SELECT";
    public const TYPE_TEXTAREA = 'TEXTAREA';
    public const TYPE_NUMBER = 'NUMBER';
    public const TYPE_DATE = 'DATE';
    public const TYPE_INAMGE = 'INAMGE';
    public const TYPE_INAMGES = 'INAMGES';
    public const TYPE_EDITOR = 'EDITOR';
    
    private $key = '';
    private $label = '';
    private $type = '';
    private $placeholder = '';
    private $default;
    private $options = [];
    private $span; // 24格
    private $required = false;
    private $rows = 5;

    public static function createInput($key, $label) {
        $item = self::create(self::TYPE_INPUT, $key, $label);
        $item->setPlaceholder('请输入' . $label);
        return $item;
    }

    public static function createDate($key, $label) {
        $item = self::create(self::TYPE_DATE, $key, $label);
        $item->setPlaceholder('请选择' . $label);
        return $item;
    }

    public static function createTextarea($key, $label, $rows = 5) {
        $item = self::create(self::TYPE_TEXTAREA, $key, $label);
        $item->setPlaceholder('请输入' . $label);
        $item->rows = $rows;
        return $item;
    }

    public static function createEditor($key, $label) {
        $item = self::create(self::TYPE_EDITOR, $key, $label);
        $item->setPlaceholder('请输入' . $label);
        return $item;
    }

    public static function createNumber($key, $label) {
        $item = self::create(self::TYPE_NUMBER, $key, $label);
        $item->setPlaceholder('请输入' . $label);
        return $item;
    }

    public static function createImage($key, $label) {
        $item = self::create(self::TYPE_INAMGE, $key, $label);
        return $item;
    }

    public static function createImages($key, $label) {
        $item = self::create(self::TYPE_INAMGES, $key, $label);
        return $item;
    }

    public static function createSelect($key, $label, $options) {
        $item = self::create(self::TYPE_SELECT, $key, $label);
        $item->setPlaceholder('请选择' . $label);
        $item->setOptions($options);
        return $item;
    }

    public static function create($type, $key, $label) {
        $builder = new FormField();
        $builder->key = $key;
        $builder->type = $type;
        $builder->label = $label;
        return $builder;
    }

    public function setPlaceholder($placeholder) {
        $this->placeholder = $placeholder;
        return $this;
    }

    public function setOptions($options) {
        $this->options = $options;
        return $this;
    }

    public function setRequired($required = true) {
        $this->required = $required;
        return $this;
    }

    public function setDefault($default) {
        $this->default = $default;
        return $this;
    }

    public function setSpan($span) {
        $this->span = $span;
        return $this;
    }

    public function getSpan() {
        return $this->span;
    }

    public function build() {
        $option = [
            'type' => $this->type,
            'key' => $this->key,
            'label' => $this->label,
            'placeholder' => $this->placeholder,
            'options' => $this->options,
            'required' => $this->required,
            'rows' => $this->rows,
        ];
        if(isset($this->span)) $option['SPAN'] = $this->span;
        if(isset($this->default)) $option['default'] = $this->default;
        return $option;
    }
}
